<template>
  <div class="settings-container">
    <div class="settings-header">
      <h2>系统设置</h2>
    </div>

    <div class="settings-tabs">
      <div
        v-for="(tab, index) in tabs"
        :key="index"
        :class="['tab-item', {'active': currentTab === tab.id}]"
        @click="currentTab = tab.id"
      >
        {{ tab.title }}
      </div>
    </div>
    
    <div class="settings-content">
      <!-- 账户信息 -->
      <div v-if="currentTab === 'account'" class="setting-panel">
        <!-- <h3 class="panel-title">账户信息</h3> -->

        <div class="setting-item">
          <div class="item-label">用户名</div>
          <div class="item-content">
            <span>{{ userInfo.username }}</span>
            <button @click="showAdminNotice">修改</button>
          </div>
        </div>
        <div class="setting-item"> 
          <div class="item-label">年龄</div>
          <div class="item-content">
            <span>{{ userInfo.age }}</span>
            <button @click="showAdminNotice">修改</button>
          </div>
        </div>
        <div class="setting-item"> 
          <div class="item-label">性别</div>
          <div class="item-content">
            <span>{{ userInfo.sex }}</span>
            <button @click="showAdminNotice">修改</button>
          </div>
        </div>
        <div class="setting-item">
          <div class="item-label">绑定手机</div>
          <div class="item-content">
            <span>{{ userInfo.phone || '未绑定' }}</span>
            <button @click="showAdminNotice">{{ userInfo.phone ? '更换手机' : '绑定手机' }}</button>
          </div>
        </div>
<!-- 
        <div class="setting-item">
          <div class="item-label">修改密码</div>
          <div class="item-content">
            <span>{{ userInfo.password }}</span>
            <button @click="showAdminNotice">修改密码</button>
          </div>
        </div> -->

        <div class="setting-item">
          <div class="item-label">角色</div>
          <div class="item-content">
            <span>{{ Array.isArray(userInfo.roles) ? userInfo.roles.join(', ') : userInfo.roles }}</span>
            <button @click="showAdminNotice">修改</button>
          </div>
        </div>
      </div>

    </div>

     <!-- ✅ 模态对话框（Element Plus） -->
    <el-dialog
      :visible.sync="showModal"
      title="提示"
      width="400px"
      :close-on-click-modal="false"
      :show-close="false"
    >
      <p>账户信息仅由管理员维护，如需修改请联系系统管理员。</p>
      <template #footer>
        <el-button type="primary" @click="showModal = false">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import {getUserById} from '@/api/authority'
import { getUser } from '@/utils/storage'

export default {
  name: 'Settings',
  data() {
    return {
      //  avatar: 'https://picsum.photos/100/100?random=1',
      tabs: [
        { id: 'account', title: '账户信息' },

      ],
      currentTab: 'account',
      userInfo: {
        username: 'user',   
        age: 18,
        sex: '男',
        phone: '138****1234',
        password: '123456',
        roles:['一','二','三'],

      },
      showModal: false,
    }
  },
  created() {
    this.getUserInfo()
    
  },
  methods: {
     // 所有按钮调用同一个方法
    showAdminNotice() {
      console.log('账户信息仅由管理员维护，如需修改请联系系统管理员。')
      this.showModal = true
    },
   //获取用户信息
   async getUserInfo() { 
    const user = getUser();
    const id = user.userId
      try {
        const res = await getUserById(id)
        if (res.code == 200) {
          const userDetail = res.result;
          console.log('账户信息', userDetail)
          this.userInfo.username = userDetail.username
          this.userInfo.age = userDetail.age
          this.userInfo.sex = userDetail.userGender
          this.userInfo.phone = userDetail.userPhone
          this.userInfo.roles = userDetail.roleNames
        } else {
          throw new Error(res.message || '失败')
        }
      } catch (error) {
        console.log(error)
        this.$message.error('出错啦，请稍后重试！')
      }
   },

  }
}
</script>

<style scoped>
.settings-container {
  width: 100%;
  max-width: 800px;
  margin: 0 auto;
  padding: 20px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

.settings-header {
  margin-bottom: 20px;
  border-bottom: 1px solid #eee;
  padding-bottom: 15px;
}

.settings-header h2 {
  margin: 0;
  font-size: 24px;
  color: #333;
}

.settings-tabs {
  display: flex;
  border-bottom: 1px solid #eee;
  margin-bottom: 20px;
}

.tab-item {
  padding: 10px 20px;
  cursor: pointer;
  font-size: 16px;
  color: #666;
  border-bottom: 2px solid transparent;
  transition: all 0.3s;
}

.tab-item.active {
  color: #1e88e5;
  border-bottom-color: #1e88e5;
  font-weight: 500;
}

.setting-panel {
  padding: 10px 0;
}

.panel-title {
  font-size: 20px;
  color: #333;
  margin: 0 0 20px 0;
  padding-bottom: 10px;
  border-bottom: 1px solid #f0f0f0;
}

.setting-item {
  display: flex;
  padding: 15px 0;
  border-bottom: 1px solid #f0f0f0;
}

.setting-item:last-child {
  border-bottom: none;
}

.item-label {
  width: 150px;
  font-size: 16px;
  color: #555;
  flex-shrink: 0;
}

.item-content {
  flex-grow: 1;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
}

.item-content span {
  margin-right: 15px;
  color: #333;
}

.item-content button {
  background-color: #f0f0f0;
  border: none;
  padding: 6px 12px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
  color: #555;
  transition: background-color 0.3s;
}

.item-content button:hover {
  background-color: #e0e0e0;
}

.avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  object-fit: cover;
  margin-right: 15px;
}

.channel-options, .notification-options, .reminder-options, .layout-options, .privacy-options, .data-sharing-options {
  display: flex;
  flex-wrap: wrap;
  gap: 15px;
}

.channel-options label, .notification-options label, .reminder-options label, .layout-options label, .privacy-options label, .data-sharing-options label {
  display: flex;
  align-items: center;
  cursor: pointer;
  font-size: 14px;
  color: #333;
}

.channel-options input, .notification-options input, .reminder-options input, .layout-options input, .data-sharing-options input {
  margin-right: 8px;
}

.privacy-options select {
  margin-left: 10px;
  padding: 4px 8px;
  border: 1px solid #ddd;
  border-radius: 4px;
  background-color: #fff;
  font-size: 14px;
  color: #555;
}

.item-content select {
  padding: 6px 12px;
  border: 1px solid #ddd;
  border-radius: 4px;
  background-color: #fff;
  font-size: 14px;
  color: #555;
}

.font-size-slider {
  display: flex;
  align-items: center;
  width: 250px;
}

.font-size-slider input {
  flex-grow: 1;
  margin-right: 15px;
}

.settings-footer {
  display: flex;
  justify-content: flex-end;
  gap: 15px;
  margin-top: 30px;
  padding-top: 20px;
  border-top: 1px solid #eee;
}

.save-btn {
  background-color: #1e88e5;
  color: white;
  border: none;
  padding: 10px 20px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 16px;
  transition: background-color 0.3s;
}

.save-btn:hover {
  background-color: #0d47a1;
}

.cancel-btn {
  background-color: #f0f0f0;
  color: #555;
  border: none;
  padding: 10px 20px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 16px;
  transition: background-color 0.3s;
}

.cancel-btn:hover {
  background-color: #e0e0e0;
}
</style>
